using System;
using System.Collections.Generic;
using System.Text;
using CodePlex.Resourceful;
using CodePlex.SpaceBlock.FS;
using CodePlex.Resourceful.Amazon.S3;
using CodePlex.SpaceBlock.UI.Test;
using CodePlex.SpaceBlock.UI.Controls;
using System.Windows.Forms;

namespace CodePlex.SpaceBlock.UI
{
    public class FileSystemManager
    {

        private readonly ReadOnlyList<FileSystemInfo> _fileSystemInfos;
        private ReadOnlyList<IFileSystem> _fileSystems;

        public FileSystemManager(IEnumerable<FileSystemInfo> infos)
        {

            try {
                _fileSystemInfos=  new ReadOnlyList<FileSystemInfo>(infos);

                _fileSystems = _fileSystemInfos.ConvertAll<IFileSystem>(this.ToFileSystem);

                List<IFileSystem> list = new List<IFileSystem>(_fileSystems);

                _fileSystems = new ReadOnlyList<IFileSystem>(list);

        

            }
            catch
            {

            }
           
        }


        public void Validate(ProgressControl pc)
        {

            int i = 0;
            pc.SetProgress(i, _fileSystems.Count);
            foreach (IFileSystem fs in _fileSystems)
            {
                i++;

                pc.SetProgressText("Validating file systems... ({0} of {1})", i, _fileSystems.Count);
                string c = fs.Caption;
                pc.SetProgress(i, _fileSystems.Count);
            }
            pc.SetProgressText("Done");

                     
        }

        public bool IsValid
        {
            get
            {
                return _fileSystems != null;
            }
        }

        public ReadOnlyList<IFileSystem> FileSystems
        {
            get
            {
                return _fileSystems;
            }
        }

        public ReadOnlyList<FileSystemInfo> FileSystemInfos
        {
            get
            {
                return _fileSystemInfos;
            }
        }


        public IFileSystem ToFileSystem(FileSystemInfo info)
        {
            if (info.Type == "Local")
            {
                return new LocalFileSystem();
            }
            if (info.Type == "S3")
            {
                string accessKeyID = info.PublicSettings.GetIfExists(UIConstants.S3Settings.AWSAccessKeyID);
                string secretAccessKey = info.PrivateSettings.GetIfExists(UIConstants.S3Settings.AWSSecretAccessKey);
                string useSSL = info.PublicSettings.GetIfExists(UIConstants.S3Settings.UseSSL);
                string defaultTransferACL = info.PublicSettings.GetIfExists(UIConstants.S3Settings.DefaultTransferACL);

                S3Connection cn = new S3Connection(accessKeyID, secretAccessKey, useSSL == true.ToString(), RequestStyle.VHost);
                return new S3FileSystem(cn,S3DefaultTransferACL.Parse(defaultTransferACL));
            }
            if (info.Type == "Nirvanix")
            {
                string appKey = info.PublicSettings.GetIfExists(UIConstants.NirvanixSettings.AppKey);
                string appName = info.PublicSettings.GetIfExists(UIConstants.NirvanixSettings.AppName);
                string username = info.PublicSettings.GetIfExists(UIConstants.NirvanixSettings.Username);
                string password = info.PrivateSettings.GetIfExists(UIConstants.NirvanixSettings.Password);
                string useSSL = info.PublicSettings.GetIfExists(UIConstants.NirvanixSettings.UseSSL);

                return new NirvanixFileSystem(appKey,appName, username, password, useSSL == true.ToString());
            }

            throw CodePlexException.Format(Properties.Messages.Exceptions_UnknownFileSystem, info.Type);

        }

    }
}
